package com.bjpowernode.crm.workbench.service.impl;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.ActivityRemark;
import com.bjpowernode.crm.workbench.mapper.ActivityRemarkMapper;
import com.bjpowernode.crm.workbench.service.ActivityRemarkService;
import lombok.Getter;
import lombok.Setter;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Getter
@Setter
public class ActivityRemarkServiceImpl implements ActivityRemarkService {

    /**
     * 访问市场活动评论数据
     */
    private ActivityRemarkMapper activityRemarkMapper;

    /**
     * 处理用户相关业务
     */
    private UserService userService;

    @Override
    public List<ActivityRemark> queryActivityRemarksByActivityId(String activityId) {
        List<ActivityRemark> activityRemarks = this.activityRemarkMapper.selectActivityRemarksByActivityId(activityId);

        //数据库中保存的是创建者和最后一个编辑者的 id ，查询结果中需要的是他们的名称
        for (ActivityRemark everyRemark : activityRemarks) {
            everyRemark.setCreateBy(this.userService.queryNameById(everyRemark.getCreateBy()));
            everyRemark.setEditBy(this.userService.queryNameById(everyRemark.getEditBy()));
        }
        return activityRemarks;
    }

    @Override
    public void saveActivityRemark(ActivityRemark activityRemark, HttpSession session) {
        User user = (User) session.getAttribute(Contants.SESSION_USER);
        String now = DateUtils.formatDateTime(new Date());

        activityRemark.setId(UUIDUtils.getUUID());
        activityRemark.setCreateBy(user.getId());
        activityRemark.setCreateTime(now);
        activityRemark.setEditBy(user.getId());
        activityRemark.setEditTime(now);
        activityRemark.setEditFlag(Contants.REMARK_EDIT_FLAG_NO_EDITED);

        this.activityRemarkMapper.insertActivityRemark(activityRemark);
    }

    @Override
    public void deleteActivityRemarkById(String id) {
        this.activityRemarkMapper.deleteActivityRemarkById(id);
    }

    @Override
    public void editActivityRemarkById(ActivityRemark activityRemark, HttpSession session) {
        User user = (User) session.getAttribute(Contants.SESSION_USER);

        activityRemark.setEditBy(user.getId());
        activityRemark.setEditTime(DateUtils.formatDateTime(new Date()));
        activityRemark.setEditFlag(Contants.REMARK_EDIT_FLAG_YES_EDITED);

        this.activityRemarkMapper.updateNoteContentById(activityRemark);
    }

}
